package com.android.smsmessing.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
	static Calendar calendar = Calendar.getInstance();
	/**
	 * 时间比对 如果firstDate > secondDate返回ture,否则返回false
	 * 
	 * @param firstDate
	 * @param secondDate
	 * @return
	 */
	public boolean compareDate(Date firstDate, Date secondDate) {
		return firstDate.getTime() > secondDate.getTime();
	}
	
	/**
	 * 天数对比
	 * 只关心天数
	 * 比如昨天晚上到今天中午,那么返回0
	 * @param firstDate		第一个时间
	 * @param secondDate	第二个时间
	 * @return
	 */
	public static int compareDay(Date firstDate, Date secondDate) {
		  if (null == secondDate || null == firstDate) {
	           return -1;
	       }
	       long intervalMilli = firstDate.getTime() - secondDate.getTime();
	       return (int) (intervalMilli / (24 * 60 * 60 * 1000));
	    } 

	/**
	 * 增加月数
	 * @param	Date:time
	 * @param	int:month
	 */
	public static void addMonth(Date time, int month) {
		calendar.setTime(time);
		calendar.add(Calendar.MONTH, month);
		time.setTime(calendar.getTime().getTime());
	}

	/**
	 * 增加天数
	 * @param	Date:time
	 * @param	int:day
	 */
	public  static void addDay(Date time, int day) {
		calendar.setTime(time);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		time.setTime(calendar.getTime().getTime());
	}

	/**
	 * 增加分钟
	 * @param	Date:time
	 * @param	int:minute
	 */
	public static void addMinute(Date time, int minute) {
		calendar.setTime(time);
		calendar.add(Calendar.MINUTE, minute);
		time.setTime(calendar.getTime().getTime());
	}
	
	/**
	 * 显示时间
	 * 如果是今天,则显示时间	如:15:36
	 * 如果是昨天 则显示昨天	
	 * 如果是前天 则显示前天
	 * 如果大于前天 则显示日期 如:02-06
	 * @param firstDate		当前时间
	 * @param secondDate	历史时间
	 * @return
	 */
	public static String displayTime(Date firstDate,Date secondDate){
		if(compareDay(firstDate, secondDate) == 0){
			return secondDate.getHours() + ":" + secondDate.getMinutes();
		}else if(compareDay(firstDate, secondDate) == 1){
			return "昨天";
		}else if(compareDay(firstDate, secondDate) == 2){
			return "前天";
		}else{
			Calendar firstCalendar = GregorianCalendar.getInstance();
			firstCalendar.setTime(firstDate); 
			
			Calendar secondCalendar = GregorianCalendar.getInstance();
			secondCalendar.setTime(secondDate); 
			
			int month =secondCalendar.get(Calendar.MONTH) +1;
			int day = secondCalendar.get(Calendar.DAY_OF_MONTH);
			String displayMonth =String.valueOf(month);
			String displayDay = String.valueOf(day);
			if(month < 10){
				displayMonth = "0" + displayMonth;
			}
			
			if(day < 10){
				displayDay = "0" + displayDay;
			}
			return displayMonth + "-" + displayDay;
		}
	}
}
